package com.liuf.di.controller;

import com.liuf.di.common.CommonResponse;
import com.liuf.di.common.ResponseUtil;
import com.liuf.di.service.IDatabaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @program: spring-cloud-liuf-master
 * @description: 元数据库管理
 * @author: Mr.Liu
 * @create: 2018-09-04 14:33
 * @码云： https://gitee.com/www.jiandan.com
 **/
@Api(value = "DI-DataBase",description = "元数据管理API管理")
@RestController
@RequestMapping(value = "/api/kettle")
public class DatabaseController {

    /**日志*/
//    private static final Logger logger = LoggerFactory.getLogger(DatabaseController.class);
    /**
     * 初始化Map参数
     */
    private Map<String,Object> params = null;

    @Autowired
    private IDatabaseService databaseService;

    @ApiOperation(value = "查询数据库连接信息")
    @GetMapping(value = "/database/select")
    public CommonResponse getDatabases(@ApiParam(value = "分页参数-页数")@RequestParam(name = "pageNum", defaultValue = "0") int pageNum,
                                       @ApiParam(value = "分页参数-条数")@RequestParam(name = "pageSize", defaultValue = "10") int pageSize,
                                       @ApiParam(value = "元数据名称")@RequestParam(required = false) String name) throws Exception{
        params = new HashMap<>();
        params.put("pageSize",pageSize);
        params.put("pageNum",pageNum);
        params.put("name",name);
        return ResponseUtil.generateResponse(databaseService.getDatabases(params));
    }

    @ApiOperation(value = "获取单一数据源")
    @GetMapping(value = "/database/{id}")
    public CommonResponse getdatabase(@ApiParam(value = "主键ID")@PathVariable long id, HttpServletRequest request, HttpServletResponse response) throws Exception{
        return ResponseUtil.generateResponse(databaseService.getDatabase(id));
    }

    @ApiOperation(value = "存储与修改数据源")
    @PostMapping(value = "/database/saveOrUpdate")
    public CommonResponse save(@ApiParam(value = "数据源基础信息")@RequestBody Map<String,String> params,HttpServletRequest request,HttpServletResponse response)throws Exception{
        return ResponseUtil.generateResponse(databaseService.save(params)==null?false:true);
    }

    @ApiOperation(value = "删除数据源")
    @DeleteMapping(value = "/database/delete/{name}")
    public CommonResponse delete(@ApiParam(value = "元数据名称") @PathVariable String name,HttpServletRequest request,HttpServletResponse response)throws Exception{
        return ResponseUtil.generateResponse(databaseService.detele(name));
    }

    @ApiOperation(value = "查询用户定义的数据库的详细信息列表")
    @GetMapping(value = "/database/attribute")
    public CommonResponse findDatabaseAttribute(@ApiParam(value = "参数") @RequestBody(required = false) Map<String,Object> params,HttpServletRequest request,HttpServletResponse response)throws Exception{
        return ResponseUtil.generateResponse(databaseService.getDatabaseAttribute(params));
    }

    @ApiOperation(value = "查询数据库的连接方式列表")
    @GetMapping(value = "/database/contype")
    public CommonResponse findDatabaseContype(@ApiParam(value = "参数") @RequestBody(required = false) Map<String,Object> params,HttpServletRequest request,HttpServletResponse response)throws Exception{
        return ResponseUtil.generateResponse(databaseService.getDatabaseContype(params));
    }

    @ApiOperation(value = "查询kettle支持的所有的数据库类型列表")
    @GetMapping(value = "/database/type")
    public CommonResponse findDatabaseType(@ApiParam(value = "参数") @RequestBody(required = false) Map<String,Object> params,HttpServletRequest request,HttpServletResponse response)throws Exception{
        return ResponseUtil.generateResponse(databaseService.getDatabaseType(params));
    }

    @ApiOperation(value = "查询元数据名称是否存在[存在将对其修改OR不存在就进行存储]")
    @GetMapping(value = "/database/exist/{name}")
    public CommonResponse findBooleanDataBase(@ApiParam(value = "元数据数据库名称")@PathVariable String name, HttpServletRequest request, HttpServletResponse response)throws Exception{
        return ResponseUtil.generateResponse(databaseService.booleanDataBase(name));
    }
}
